package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_mylist;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class CaixintonglistActivity extends Activity implements OnItemClickListener {

	private ListView mListView;
	private MyApp application;
	private ArrayList<Data_mylist> datalist = new ArrayList<Data_mylist>();
	private View mBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_caixintonglist);
		
		application = (MyApp) getApplication();
		mBar = findViewById(R.id.avcaixintonglist_progress_bar);
		findViewById(R.id.avcaixintonglist_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				finish();
			}
		});
		
		mListView = (ListView) findViewById(R.id.avcaixintonglist_listview);
		mListView.setOnItemClickListener(this);
		getlistlistdata();
		
	}
	
	private void getlistlistdata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				CaixintonglistActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "moneyLoglist");
		if(application.getDatas_load ()!=null){
		params.put("userid", application.getDatas_load().getId());
		}
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				 Log.e("财信通","response"+response);
				try {
					String flag = response.getString("flag");
					if (flag.equals("success")) {
						JSONObject jsonObject = response.getJSONObject("data");
						JSONArray jsonArray = jsonObject.getJSONArray("data");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject object = jsonArray.getJSONObject(i);
							long create_time = object.getLong ("create_time");
							String money = object.getString("money");
							String id = object.getString("id");
							String type = object.getString("type");
							String status = object.getString("status");
							String note = object.getString("note");
							double actual_money = object.getDouble ("actual_money");
							double curr_money = object.getDouble ("curr_money");
							String fee="";
							if(object.has ("fee")){
								fee = object.getString ("fee");
							}

							if(type.equals("6")){
							Data_mylist list = new Data_mylist (create_time, money, id, type, status, note, actual_money, curr_money, fee);
								datalist.add(list);
							}
							
						}
					} else if (flag.equals("unknow")) {
						String info = response.getString("info");
						Toast.makeText(CaixintonglistActivity.this, info,Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
				if(datalist.size() > 0){
					Myadapter adapter = new Myadapter();
					mListView.setAdapter(adapter);
				}
				
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(CaixintonglistActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show();
//				Log.e("������ǰ�dd", "ʧ��" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				
				mBar.setVisibility(View.VISIBLE);
				
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
//				Log.e("������ǰ�", "���");
			}
		});
	}
	
	class Myadapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datalist.size();
		}

		@Override
		public Object getItem(int i) {
			// TODO Auto-generated method stub
			return i;
		}

		@Override
		public long getItemId(int i) {
			// TODO Auto-generated method stub
			return i;
		}
		
		class Holder {
			TextView mTname;
			TextView mTmoney;
			TextView mTtime;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = getLayoutInflater().inflate(R.layout.item_adter_mylist, null);
				holder.mTname = (TextView) convertView.findViewById(R.id.item_mylist_name);
				holder.mTmoney = (TextView) convertView.findViewById(R.id.item_mylist_money);
				holder.mTtime = (TextView) convertView.findViewById(R.id.item_mylist_time);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			
			Data_mylist list = datalist.get(position);
			String status = list.status;
			String type = list.type;
//			if(type.equals("1")){
//				holder.mTname.setText("��ֵ");
//			}else if(type.equals("2")){
//				holder.mTname.setText("����");
//			}else if(type.equals("3")){
//				holder.mTname.setText("�вƽ��");
//			}else if(type.equals("4")){
//				holder.mTname.setText("�в����");
//			}else{
				holder.mTname.setText(list.note);
//			}

			SimpleDateFormat sdf = new SimpleDateFormat ("yyyy年MM月dd日HH时");
			holder.mTtime.setText(sdf.format (new Date (list.create_time*1000L)));
//			if(status.equals("1")){
				holder.mTmoney.setText(list.money);
//			}else{
//				holder.mTmoney.setText("+"+list.money);
//			}
			return convertView;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(CaixintonglistActivity.this,Zcailist_infoActivity.class);
		intent.putExtra("id", datalist.get(arg2).id);
		startActivity(intent);
	}
}
